package org.wanho.shop.dao.impl;

import org.wanho.jdbc.JdbcTemplate;
import org.wanho.shop.dao.ItemDao;
import org.wanho.shop.entity.Item;
import org.wanho.shop.mapper.ItemMapper;
import org.wanho.util.RowMapper;

import java.util.List;

/**
 * Author：汤小洋
 * Date：2023-04-27 10:55
 * Description：<描述>
 */
public class ItemDaoImpl implements ItemDao {

    private JdbcTemplate<Item> jt = new JdbcTemplate<>();
    private RowMapper<Item> rm = new ItemMapper();

    @Override
    public void insert(Item item) {
        String sql = new StringBuffer()
                .append(" insert into t_item ")
                .append("   (product_id,num,price,order_id) ")
                .append(" values ")
                .append("   (?,?,?,?) ")
                .toString();
        jt.update(sql,item.getProduct().getId(),item.getNum(),item.getPrice(),item.getOrder().getId());
    }

    @Override
    public List<Item> selectById(int orderId) {
        String sql = new StringBuffer()
                .append(" select ")
                .append("   i.id, ")
                .append("   i.num, ")
                .append("   i.price, ")
                .append("   p.name ")
                .append(" from ")
                .append("   t_item i inner join t_product p on i.product_id=p.id ")
                .append(" where ")
                .append("   i.order_id=? ")
                .toString();
        return jt.query(sql, rm, orderId);
    }
}
